﻿ using System;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Runtime.CompilerServices;
using System.Threading;

namespace Lon.Server
{
   
    public class DataPackageProcessService
    {
        protected Dictionary<int, DataProcessProvider> dataProcessProviderTbl = new Dictionary<int, DataProcessProvider>();

   

        protected List<DataFrame> requestPackagList = new List<DataFrame>();
        protected List<DataFrame> requestPackagListInMainThread = new List<DataFrame>();


        protected DataProcessProvider GetDataPackageProcessProvider(DataFrame rPkg)
        {
            if (rPkg == null)
            {
                return null;
            }
            int id = (int)rPkg.MajorDataType;
            if (this.dataProcessProviderTbl.ContainsKey(id))
            {
                return this.dataProcessProviderTbl[id];
            }
            return null;
        }

  

        Thread dealThread;
       
     
      
        public   bool Start()
        {
           
            isRun = true;
            dealThread=new Thread(new ThreadStart(ProcessDataPackageTimerProc));
            dealThread.IsBackground=true;
            dealThread.Start();
          
            return true;
        }

        private void ProcessDataPackage(DataFrame pkg)
        {
         
                DataProcessProvider provider = this.GetDataPackageProcessProvider(pkg);
                if (provider == null) return;
                provider.Process(pkg);
        }
        bool isRun;
        private void ProcessDataPackageTimerProc()
        {     
            while(isRun)
            {
              
                try
                {
                    DataFrame[] dfs;
                    lock (cacheQueue)
                    {
                       dfs= cacheQueue.ToArray();
                       cacheQueue.Clear();
                    }
                    if (dfs == null)
                    {
                        Thread.Sleep(100);
                        continue;
                    }
                    for (int i = 0; i < dfs.Length; i++)
                    {
                        this.ProcessDataPackage(dfs[i]);
                    }
                    
                }
                catch (Exception e)
                {
                  
                }
                Thread.Sleep(100);
            }
        }

        public bool RegDataPackageProcessProvider(int majorDataType, DataProcessProvider queryProvider)
        {
            if (queryProvider == null)
            {
                return false;
            }        
            if (this.dataProcessProviderTbl.ContainsKey(majorDataType))
            {
                return false;
            }
            this.dataProcessProviderTbl.Add(majorDataType, queryProvider);
           
            return true;
        }


   

        Queue<DataFrame> cacheQueue = new Queue<DataFrame>(500);
        public void ProcessDataFrame(DataFrame df)
        {
            lock (cacheQueue)
            {
                cacheQueue.Enqueue(df);
            }
        }


    }
}

